import React from 'react';
import { Route, Redirect } from 'react-router-dom';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import Login from './containers/Login/Login';
import Share from './containers/Share/Share';
import ResetPassword from './containers/ResetPassword/ResetPassword';
import FileShareMobileWX from './containers/FileShareMoble_wx/FileShareMobile';
import FileShareMobile from './containers/FileShareMobile/FileShareMobile';
import WebBind from './containers/WebBind/WebBind';
import Loading from './components/Loading/Loading';
import LoadingOffice from './components/Loading/LoadingOffice';
import LoadingCalendar from './components/Loading/LoadingCalendar';
import LoadingDocView from './components/Loading/LoadingDocView';
import './styles/index.scss';

const App = () => (
  <div className="app-view">
    <Route exact path="/" component={Login} />
    <Route exact path="/login" component={Login} />
    <PrivateRoute path="/dashboard" component={Share} />
    <Route path="/resetPassword" component={ResetPassword} />
    <Route path="/fileShareMobileWX/:shareId/:sharedBy" component={FileShareMobileWX} />
    <Route path="/fileShareMobile/:shareId/:sharedBy" component={FileShareMobile} />
    <Route path="/view/share/:shareId/:sharedBy" component={FileShareMobileWX} />
    <Route path="/share/webBind" component={WebBind} />
    <Route path="/webBind" component={WebBind} />
    <Route path="/loading" component={Loading} />
    <Route path="/loadingOffice" component={LoadingOffice} />
    <Route path="/loadingCalendar" component={LoadingCalendar} />
    <Route path="/loadingDocView" component={LoadingDocView} />
  </div>
);

const PrivateRoute = ({ component: Component, ...rest }) => {
 const hasAuthed = window.localStorage.getItem('accessToken');
  return (<Route
    {...rest}
    render={
        props => (
          hasAuthed ? (
            <Component {...props} />
          ) : (
            <Redirect
              to={{
                pathname: '/login',
                state: { from: props.location },
              }}
            />
          )
        )
      }
  />);
};

PrivateRoute.propTypes = {
  component: PropTypes.func.isRequired,
  location: PropTypes.object, // eslint-disable-line
};

export default connect()(App);
